<template>
	<view v-if="template && !loading">
		<s-layout title="商城" navbar="custom" :showLeftButton="false" tabbar="/pages/index/index"
			:bgStyle="template.style?.background" :navbarStyle="template.style?.navbar"
			:onShareAppMessage="state.shareInfo">

			<s-block v-for="(item, index) in template.data" :key="index" :type="item.type"  :styles="item.style">

				<!-- <s-block-item :type="item.type" :data="item.data" :styles="item.style" /> -->
				<s-block-item :type="item.type" :data="item.data" :styles="item.style"></s-block-item>
			</s-block>
			<!-- 购物车 -->

			<view class="cart ss-flex ss-row-center" @click="sheep.$router.go('/pages/etc/cart')" v-if="!isAuditMode">
				<uni-icons type="cart-filled" color="#F46800" size="35"></uni-icons>
			</view>
			<!-- 广告模块 -->
			<!-- <s-popup-image /> -->
		</s-layout>
	</view>
</template>

<script setup>
	import {
		computed,
		ref,
		provide,
		reactive
	} from 'vue';
	import {
		onLoad,
		onPageScroll,
		onPullDownRefresh,
		onShareTimeline
	} from '@dcloudio/uni-app';
	import sheep from '@/sheep';
	import appApi from '@/sheep/api/app';
	import $share from '@/sheep/platform/share';
	import mitt from '@/sheep/mitt.js';
	import {
		showShareModal
	} from '@/sheep/hooks/useModal';
	const isAuditMode = computed(() => sheep.$store('app').isAuditMode);
	// 隐藏原生tabBar
	uni.hideTabBar({
		fail: () => {},
	});

	const state = reactive({
		shareInfo: {},
		goods_id: '',
	})

	const loading = ref(false);

	const template = computed(() => sheep.$store('app').template?.home);
	// const template = ref({});

	const handleShareInfo = (value) => {
		state.goodsInfo = value;

		showShareModal();
	};

	provide('handleShareInfo', handleShareInfo);

	onLoad((options) => {
		// init();
		// #ifdef MP
		// 小程序识别二维码
		if (options.scene) {
			const sceneParams = decodeURIComponent(options.scene).split('=');
			options[sceneParams[0]] = sceneParams[1];
		}
		// #endif

		// 预览模板
		if (options.templateId) {
			sheep.$store('app').init(options.templateId);
		}

		// 解析分享信息
		if (options.spm) {
			$share.decryptSpm(options.spm);
		}

		// 进入指定页面(完整页面路径)
		if (options.page) {
			sheep.$router.go(decodeURIComponent(options.page));
		}
		//分享触发方法
		mitt.on('indexGoodShare', function(data) {
			state.goods_id = data.good.id;
			state.shareInfo = sheep.$platform.share.getShareInfo({
				title: data.good.title,
				image: sheep.$url.cdn(data.good.image),
				params: {
					page: '2',
					query: data.good.id,
				},
			}, {
				id: data.good.id,
				type: 'goods', // 商品海报
				title: data.good.title, // 商品标题
				image: sheep.$url.cdn(data.good.image), // 商品主图
				price: data.good.price[0], // 商品价格
				original_price: data.good.original_price, // 商品原价
			}, );
			
			showShareModal();
		})
	});

	// 下拉刷新
	onPullDownRefresh(async () => {
		loading.value = true;
		uni.showLoading({
			title: '正在刷新...',
			mask: true,
			fail: () => {
				uni.hideLoading();
			},
		});
		await sheep.$store('app').init();
		setTimeout(function() {
			uni.stopPullDownRefresh();
			loading.value = false;
			uni.hideLoading();
		}, 800);
	});

	onPageScroll(() => {});

	async function init() {


		if (sheep.$store('app').isAuditMode == true) {
			const res = await sheep.$api.merchant.init(sheep.$store('app').audit_versions);
			if (res.error == 0) {
				let templateHome = res.data.template.home;
				templateHome.style.navbar.list.mp[0].text = '商城';
				template.value = templateHome;
			}
		} else {
			const res = await sheep.$api.merchant.init();

			if (res.error == 0) {

				let templateHome = res.data.template.home;
				templateHome.style.navbar.list.mp[0].text = '商城';
				template.value = templateHome;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cart {
		position: fixed;
		z-index: 100;
		right: 50rpx;
		bottom: 220rpx;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		background: #ffffff;
		box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.13);
	}
</style>